<template>
  <el-drawer v-model="drawer" title="关联订单" size="50%">
    <el-form
      class="-mb-15px"
      :model="orderQueryParams"
      ref="queryOrderFormRef"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="订单号" prop="orderCode">
        <el-input v-model="orderQueryParams.orderCode" placeholder="请输入订单号" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="orderList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="订单类型" align="center">
        <template #default="scope">
          {{ (scope.row.detail.type = '001' ? '美发订单' : '到家订单') }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="detail.code" />
      <el-table-column label="订单状态" align="center" prop="detail.status" />
      <el-table-column label="业务发生位置" align="center" prop="detail.address" />
      <el-table-column label="订单营业额(元)" align="center">
        <template #default="scope">
          {{ scope.row.detail.actualPaymentCNY }}
        </template>
      </el-table-column>
      <el-table-column label="订单分账详情" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="getOrderSubList(scope.row.orderCode)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="orderTotal"
      v-model:page="orderQueryParams.pageNo"
      v-model:limit="orderQueryParams.pageSize"
      @pagination="getOrderList"
    />
  </el-drawer>
  <Dialog title="订单详情" v-model="dialogVisible" class="wallet-dialog">
    <el-table :data="orderSubList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="子订单号" align="center" prop="code" />
      <el-table-column label="分账人员" align="center" prop="remark.username" />
      <el-table-column label="分账方" align="center" prop="remark.roleName" />
      <el-table-column label="比例" align="center">
        <template #default="scope"> {{ scope.row.remark.proportion * 100 }} % </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="remark.moneyCNY">
        <template #default="scope"> {{ scope.row.remark.moneyCNY }} 元 </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="orderSubTotal"
      v-model:page="orderSubQueryParams.pageNo"
      v-model:limit="orderSubQueryParams.pageSize"
      @pagination="orderSubList"
    />
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as DivideApi from '@/api/meijia/divide'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined
})
const dialogVisible = ref(false) // 弹窗的是否展示
const activeName = ref('first')
const drawer = ref(false)
const queryOrderFormRef = ref() // 搜索的表单
const orderList = ref([]) // 订单列表
const orderTotal = ref(0) // 订单列表的总页数
const orderQueryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  divideAllCode: undefined
})
const orderSubList = ref([]) // 订单列表
const orderSubTotal = ref(0) // 订单列表的总页数
const orderSubQueryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderCode: undefined
})
const divideCode = ref('')
/** 打开弹窗 */
const open = async (code: string) => {
  drawer.value = true
  divideCode.value = code
  await getOrderList()
}

const getOrderList = async () => {
  orderQueryParams.divideAllCode = divideCode.value
  const data = await DivideApi.getDivideOrderPageByAllCode(orderQueryParams)
  data.list.forEach((element) => {
    element.detail = JSON.parse(element.orderDetail)
  })
  orderList.value = data.list
  orderTotal.value = data.total
}
const getOrderSubList = async (code: string) => {
  dialogVisible.value = true
  orderSubQueryParams.orderCode = code
  const data = await DivideApi.getDivideByOrderCode(orderSubQueryParams)
  data.list.forEach((element) => {
    element.remark = JSON.parse(element.remark)
  })
  orderSubList.value = data.list
  orderSubTotal.value = data.total
}
/** 搜索按钮操作 */
const handleQuery = () => {
  orderQueryParams.pageNo = 1
  getOrderList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryOrderFormRef.value.resetFields()
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
<style>
.wallet-dialog {
  height: 70%; /* 设置您想要的固定高度 */
  overflow-y: auto; /* 对话框内容超出固定高度时自动显示滚动条 */
}
</style>
